/*
 * AccountInfoPanel.java
 *
 * Created on 2011-5-11, 21:49:26
 */
package banksimulator.ui;

import banksimulator.bank.AbstractAccount;
import banksimulator.bank.AccountChangeEvent;
import banksimulator.bank.AccountChangeListener;
import java.text.DecimalFormat;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

/**
 * 这个类用来显示显示某一帐户的常规信息，包括帐户ID，用户名，余额以及交易记录。此外，
 * 还可以用来修改并保存帐户的用户名。
 * <p>使用时只需传入一个AbstractAccount子类的具体实例即可自动显示其相关信息，
 * 当该帐户的进行了存款或取款的操作时，会自动刷新显示余额和交易记录。
 * @author Rajab
 */
public class AccountInfoPanel extends javax.swing.JPanel {

    //要显示的帐户
    private AbstractAccount account;
    //帐户交易表格模型
    private TransactionTableModel tabModel;
    //帐户余额变化的侦听器
    private AccountChangeListener listener = new AccountChangeListener() {

        public void accountChanged(AccountChangeEvent evt) {
            //显示新的余额
            txtBalance.setValue(account.getBalance());
          // System.out.printf("Account Panel:%.2f, %.2f\n", evt.getBefore(), evt.getAfter());
        }
    };

    /** 刷新显示的信息 */
    public void refresh() {

        if (account != null) {
            txtUserID.setText(account.getAccountID());
            txtUserName.setText(account.getUserName());
            txtBalance.setValue(account.getBalance());
        } else {
            txtUserID.setText("");
            txtUserName.setText("");
            txtBalance.setValue(0.0);
        }
        tabModel.setAccount(account);
    }

    /**
     * 设置要显示的帐户。
     * @param account 要显示的帐户
     */
    public void setAccount(AbstractAccount account) {
        if (this.account != account) {
            if (this.account != null) {
                //移除之前帐户的侦听器
                this.account.removeAccountChangeListener(listener);
            }
            if (account != null) {
                //注册新帐户的侦听器
                account.addAccountChangeListener(listener);
            }
            this.account = account;
            //刷新显示信息
            refresh();
        }
    }

    /**
     * 保存对帐户用户名的修改
     */
    public void saveChanges() {
        if (account == null) {
            return;
        }
        //设置新的用户名
        account.setUserName(txtUserName.getText());
    }

    /** 构造一个新的AccountInfoPanel 显示内容为空 */
    public AccountInfoPanel() {
        initComponents();
        tabModel = (TransactionTableModel) tabTransaction.getModel();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtUserName = new JTextField();
        txtUserID = new JTextField();
        lblUserID = new JLabel();
        lblUserName = new JLabel();
        lblBalance = new JLabel();
        jScrollPane1 = new JScrollPane();
        tabTransaction = new JTable();
        txtBalance = new JFormattedTextField();

        txtUserID.setEditable(false);

        lblUserID.setText("帐户ID：");

        lblUserName.setText("帐户名：");

        lblBalance.setText("余额：");

        tabTransaction.setAutoCreateRowSorter(true);
        tabTransaction.setModel(new TransactionTableModel());
        tabTransaction.setDoubleBuffered(true);
        tabTransaction.setDefaultRenderer(String.class, new TransactionTableRender());
        jScrollPane1.setViewportView(tabTransaction);

        txtBalance.setEditable(false);
        txtBalance.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(new DecimalFormat("¤#,##0.00"))));
        ((NumberFormatter) txtBalance.getFormatter()).setValueClass(Double.class);

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                    .addComponent(jScrollPane1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                    .addGroup(Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
                            .addComponent(lblUserID, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblUserName, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblBalance, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(Alignment.LEADING)
                            .addComponent(txtUserID, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                            .addComponent(txtUserName, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                            .addComponent(txtBalance, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblUserID)
                    .addComponent(txtUserID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblUserName)
                    .addComponent(txtUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(lblBalance)
                    .addComponent(txtBalance, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JScrollPane jScrollPane1;
    private JLabel lblBalance;
    private JLabel lblUserID;
    private JLabel lblUserName;
    private JTable tabTransaction;
    private JFormattedTextField txtBalance;
    private JTextField txtUserID;
    private JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
